#include "stdafx.h"
#include "libraries.h"

#include "Application.h"
#include "GUI.h"
#include "DefaultGUIStyle.h"
#include "Filesystem.h"

using namespace Cvekas;

class GUIDemo : public Application
{
public:
	GUIDemo(HINSTANCE instance, const std::string& name, const std::string& log_filename)
		:
	Application(instance, name, log_filename)
	{
		Filesystem::init();
	}

	virtual ~GUIDemo() {}

	virtual void initVideoDevice()
	{
		settings->insert("video", VideoSettings());

		(*settings)["video"]["ScreenWidth"] = 800;
		(*settings)["video"]["ScreenHeight"] = 600;
		(*settings)["video"]["VSync"] = false;
		(*settings)["video"]["Fullscreen"] = false;

		window = WindowPtr(new Window(instance, name));
		window->showCursor(false);
		video_device->init(window);
	}

	virtual void init()
	{
		sprite_batch = video_device->createSpriteBatch(main_renderer, 800, 600, 0);
		gui_style = DefaultGUIStylePtr(new DefaultGUIStyle(sprite_batch));
		gui = GUIPtr(new GUI(gui_style, input));
		setGUI(gui);

		video_device->setClearColor(Color(255, 255, 255));

		input->getMouse()->setVirtualResolution(800, 600);

		selector_items.push_back(std::string("Selection1"));
		selector_items.push_back(std::string("Selection2"));
		selector_items.push_back(std::string("The Third"));
		selector_items.push_back(std::string("1234897446"));
	}

	virtual bool update()
	{
		input->getMouse()->update();
		return true;
	}

	virtual bool render()
	{
		video_device->beginScene();

		gui->doLabel(Vector2(10.0f, 10.0f), FORMAT("FPS: %d", % Timer::getFPS()));
		gui->doButton(Vector2(10.0f, 50.0f), "GUI Button!!!");
		gui->doSwitch(Vector2(10.0f, 120.0f), "GUI Switch!!!");
		gui->doSlider(Vector2(10.0f, 190.0f));
		gui->doSelector(Vector2(10.0f, 260.0f), selector_items);

		gui->present();

		sprite_batch->present();
		main_renderer->render();

		video_device->endScene();
		main_renderer->endFrame();
		return true;
	}

private:
	SpriteBatchPtr sprite_batch;
	DefaultGUIStylePtr gui_style;
	GUIPtr gui;

	std::vector<std::string> selector_items;
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd)
{
	GUIDemo demo(hInstance, "GUITest", "GUITest.log");
	try
	{
		demo.run();
	}
	catch (std::exception& e)
	{
		MessageBox(NULL, e.what(), "Fatal error", NULL);
	}
	return 0;
}
